<template>
  <lan-dialog
    :config="addConfig"
    :formData="searchForm"
  >
    <template #slot="scope">
      <el-input
        v-model="scope.data.slot"
        class="form-input"
      ></el-input>
    </template>
    <template #imageUrl="scope">
      <image-upload
        class="image-upload-box"
        v-model="scope.data.imageUrl"
      ></image-upload>
      <div
        v-if="scope.data.imageUrl"
        class="remove-btn-box"
      >
        <el-button
          type="text"
          class="remove-btn"
          @click="removeImage"
          icon="el-icon-delete"
        >
        </el-button>
      </div>
    </template>
  </lan-dialog>
</template>

<script>
import ImageUpload from '../../../components/ImageUpload';
import { validateForm } from './service';
import { Message } from 'element-ui';
import { parseError } from '../../../utils/index';
import { createProductTypeApi } from '../../../api/productType';
import lanDialog from '../../../components/lanDialog';

export default {
  components: {
    ImageUpload,
    lanDialog
  },
  data() {
    this.formLabelWidth = '120px';
    return {
      addConfig: {
        labelWidth: 180,
        dialogVisible: false,
        title: '添加产品类型',
        type: 'add',
        inline: false,
        formList: [
          {
            name: 'name',
            label: '产品类型名称',
            type: 'input',
            placeholder: '请输入产品类型名称',
            required: true,
            tooltip: '产品类型的名称',
            width: 300
          },
          {
            name: 'description',
            label: '备注',
            type: 'input',
            placeholder: '请输入备注',
            tooltip: '关于该产品类型的备注信息',
            width: 300
          },
          {
            name: 'imageUrl',
            type: 'slot',
            label: '图片',
            tooltip: '产品类型的图, 支持标准图片格式，文件大小不能超过10M'
          }
        ],
        buttons: [
          {
            text: '取消',
            icon: '',
            type: '',
            method: () => {
              this.addConfig.dialogVisible = false;
            }
          },
          {
            text: '确认',
            icon: '',
            type: 'primary',
            method: () => {
              this.add();
              this.addConfig.dialogVisible = false;
            }
          }
        ]
      },
      optionStatus: [
        {
          value: true,
          label: '启用'
        },
        {
          value: false,
          label: '不启用'
        }
      ],
      parameterList: [],
      dialogVisible: false,
      remoteLoading: false,
      btnLoading: false,
      searchForm: {
        name: '',
        description: '',
        imageUrl: ''
      }
    };
  },
  watch: {
    dialogVisible: {
      handler(value) {
        if (!value) this.$refs.addSearchForm.resetFields();
      }
    }
  },
  methods: {
    removeImage() {
      this.searchForm.imageUrl = '';
    },
    showDialog() {
      this.addConfig.dialogVisible = true;
    },
    add() {
      const validError = validateForm(this.searchForm);
      if (validError) return Message.warning(validError);
      this.btnLoading = true;
      createProductTypeApi(this.searchForm.name, this.searchForm.description, this.searchForm.imageUrl)
        .then(res => {
          Message.success('添加成功');
          this.dialogVisible = false;
          this.$emit('refresh');
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.btnLoading = false));
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.el-form-item {
  margin-bottom: 22px !important;
}
.input {
  width: 300px;
}
/deep/.el-icon-delete {
  font-size: 20px;
  color: red;
}
.image-upload-box {
  position: relative;
}
.remove-btn-box {
  width: 178px;
  height: 178px;
  background: rgba($color: #000000, $alpha: 0);
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  left: 24px;
  top: 0px;
  .remove-btn {
    display: none;
  }
}
.remove-btn-box:hover {
  background: rgba($color: #000000, $alpha: 0.3);
  .remove-btn {
    display: block;
  }
}
</style>
